import launch
import launch_ros
import os
from ament_index_python import get_package_share_directory
import launch_ros.parameter_descriptions

def generate_launch_description():
    #获取文件路径
    package_path=get_package_share_directory('robot_sim')
    default_urdf_path=os.path.join(package_path,'urdf/robot1','robot1.urdf.xacro')

    #为了方便修改将路径设置为参数
    action_declare_arg_model_path=launch.actions.DeclareLaunchArgument(
        name="model",default_value=str(default_urdf_path),description='机器人模型描述文件的路径'
    )

    #通过路径获取文件的内容并且提供给robot_state
    robot_description_param=launch.substitutions.Command(
    ['xacro ', launch.substitutions.LaunchConfiguration('model')]
    )

    parameter_value=launch_ros.parameter_descriptions.ParameterValue(
        robot_description_param,value_type=str)
    print(parameter_value)

    robot_state_publisher=launch_ros.actions.Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description':parameter_value}],
    )

    joint_state_publisher=launch_ros.actions.Node(
        package="joint_state_publisher",
        executable="joint_state_publisher",
    )

    rviz2=launch_ros.actions.Node(
        package='rviz2',
        executable='rviz2',
    )


    return launch.LaunchDescription([
        action_declare_arg_model_path,
        robot_state_publisher,
        joint_state_publisher,
        rviz2,

    ]

    )